package com.bluefish.bltuan.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.bluefish.bltuan.module.Category;
import com.bluefish.bltuan.module.shop.TuanGou;
import com.bluefish.bltuan.permalink.ShopPermalinkDefinition;
import com.bluefish.bltuan.service.CategoryService;
import com.bluefish.bltuan.service.ShopService;
import com.bluefish.bltuan.tools.DateUtils;

public class IndexAction extends OpenAction {

    /**
     * 
     */
    private static final long   serialVersionUID = -6350760511362784857L;

    private static final String TODAY            = "today";

    private static final String PAST             = "past";

    private static final String HELP             = "help";

    private ShopService         shopService;

    private CategoryService     categoryService;

    private TuanGou             tuan;

    private Category            currentCity;

    @Resource
    private String              defaultCity;

    @Resource
    private String              remainTimeFormat;

    private String              remain;

    private List<TuanGou>       tuans;

    @Override
    public HttpHeaders create() {
        return null;
    }

    @Override
    public String destroy() {
        return null;
    }

    @Override
    public String edit() {
        return null;
    }

    @Override
    public HttpHeaders editNew() {
        return null;
    }

    @Override
    public HttpHeaders index() {
        super.index();
        this.currentCity = this.categoryService.getCityByName(this.defaultCity);
        return goIndex();
    }

    private HttpHeaders goIndex() {
        this.tuan = this.shopService.getPublishedTuangouToday(this.currentCity);
        if (this.tuan != null)
            this.remain = DateUtils.remain(this.tuan.getExpireDate(), remainTimeFormat);
        if (this.tuan == null)
            return new DefaultHttpHeaders(INDEX).disableCaching();
        else
            return new DefaultHttpHeaders(TODAY).disableCaching();
    }

    @Override
    public HttpHeaders show() {
        if (PAST.equals(this.getId())) {
            this.currentCity = this.categoryService.getCityByName(this.defaultCity);
            return goPast();
        } else if (HELP.equals(this.getId())) {
            return new DefaultHttpHeaders(HELP).disableCaching();
        } else {
            this.currentCity = this.categoryService.getCityByNickName(this.getId());
            return goIndex();
        }
    }

    public HttpHeaders past() {
        this.currentCity = this.categoryService.getCityByNickName(this.getId());
        return goPast();
    }

    private HttpHeaders goPast() {
        this.tuans = this.shopService.listPublishedPastTuans(getPageIndex(20), this.currentCity);
        return new DefaultHttpHeaders(PAST).disableCaching();
    }

    public String shopUrl(TuanGou t) {
        ShopPermalinkDefinition definition = new ShopPermalinkDefinition(
            "/%{tuangou_city}/deal/%{tuangou_nick}.html");
        return definition.generatePermalink(t);
    }

    public String shortStr(String str, int len) {
        if (str != null && str.trim().length() > len) {
            return str.trim().substring(0, len) + "...";
        }
        return str;
    }

    @Override
    public String update() {
        return null;
    }

    public TuanGou getTuan() {
        return tuan;
    }

    public void setShopService(ShopService shopService) {
        this.shopService = shopService;
    }

    public Category getCurrentCity() {
        return currentCity;
    }

    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    public void setDefaultCity(String defaultCity) {
        this.defaultCity = defaultCity;
    }

    public void setRemainTimeFormat(String remainTimeFormat) {
        this.remainTimeFormat = remainTimeFormat;
    }

    public String getRemain() {
        return remain;
    }

    public List<TuanGou> getTuans() {
        return tuans;
    }

    public String getDefaultCity() {
        return defaultCity;
    }
}
